<?php
namespace App\Models;

class User extends Base
{
    protected $hidden = ['password']; //隐藏字段

    //与角色表的远程一对多关联
    public function roles()
    {
        return $this->hasManyThrough(Role::class, UserRole::class, 'user_id', 'id', 'id');
    }

    /**
     * 返回用户拥有的所有权限
     */
    public function permits()
    {
        //先找出用户角色
        $roleids = $this->roles->pluck('id');   //将用户拥有的角色id
        //找出这些角色拥有的权限
        $permitids = RolePermit::whereIn('role_id', $roleids)->get()->pluck('permit_id')->unique();
        return Permit::whereIn('id', $permitids)->get();
    }
}
